<?php

namespace App\Http\Controllers\Backend\User;

use Illuminate\Http\Request;
use App\Http\Models\User\UserFrom;
use App\Http\Models\User\UserService;
use App\Http\Controllers\Backend\BackendController;

class UserController extends BackendController
{

    /**
     * UserController  用户控制器
     *
     * index() 用户列表以及搜索
     * create() 渲染新增页面
     * store() 新增用户
     * edit() 渲染修改页面
     * update() 修改用户信息
     * destroy() 删除用户信息
     *
     * @author Jinfeihu
     *
     * */
    private $userService;
    private $request;

    public function __construct(UserService $userService, Request $request) {
        $this->userService = $userService;
        $this->request = $request;
    }

    public function index() {
        $userDatas = $this->userService->index($this->request);
        return view("backend.user.index", [
            'userDatas' => $userDatas,
            'request' => $this->request
        ]);
    }

    public function create() {
        //$datas = $this->userService->getRule();
        return view("backend.user.create");
    }

    public function store(UserFrom $userFrom) {
        $this->userService->store($userFrom);
        return redirect('admin/user');
    }

    public function edit($id) {
        $userDatas = $this->userService->show($id);
        return view('backend.user.update', [
            'userDatas' => $userDatas
        ]);
    }

    public function update(UserFrom $userFrom, $id) {
        $this->userService->updateSave($userFrom, $id);
        return redirect('admin/user');
    }

    public function show($id) {
        $userDatas = $this->userService->show($id);
        return view("backend.user.show", [
            'userDatas' => $userDatas
        ]);
    }

    public function destroy($id) {
        return $this->userService->remove($id);
    }

    public function __destruct() {
        unset($this->userService);
        unset($this->request);
    }

}
